#!/usr/bin/python3.9
# -*- coding: utf-8 -*-
# @Time    : 2021/5/16 4:53 下午
# @Author  : 姜晖
# @Email   : jianghui@tianyancha.com
# @File    : conftest.py


import time
import allure
import pytest
from browser import Browser
from utils import load_env

browser = None


@pytest.fixture(scope='session', autouse=True)
def browser_init():
    global browser
    if browser is None:
        browser = Browser()
    return browser

@pytest.fixture(scope="class")
def get_ipsa_main_page_with_logged(browser_init, request, driver_type, browser_type):
    env = load_env()
    try:
        user = request.param
        print(user)
        username = user[env['default']][0]
        password = user[env['default']][1]
    except:
        username = env['pc'][env['default']]['username']
        password = env['pc'][env['default']]['password']
    main_page = browser_init.launch(driver=driver_type, browser=browser_type).login_successfully(username, password)
    msg=main_page.get_successmsg()
    assert msg=='注销'

    yield msg
    browser_init.quit()

@pytest.fixture(scope="class")
def get_ipsaa_main_page_with_logged(browser_init, request, driver_type, browser_type):
    env = load_env()
    try:
        username = env['ipsa'][env['default']]['username']
        password = env['ipsa'][env['default']]['password']

    except:
        username = env['pc'][env['default']]['username']
        password = env['pc'][env['default']]['password']
    main_page = browser_init.launch(driver=driver_type, browser=browser_type).login_successfully(username, password)
    yield main_page
    browser_init.quit()










@pytest.fixture(scope="class")
def get_pc_main_page_with_no_logged(browser_init, driver_type, browser_type):
    main_page = browser_init.launch(driver=driver_type, browser=browser_type)
    yield main_page
    browser_init.quit()


@pytest.fixture(scope="class")
def get_pc_main_page_with_logged_with_location(browser_init, request, driver_type, browser_type):
    env = load_env()
    try:
        user = request.param
        username = user[env['default']][0]
        password = user[env['default']][1]
    except:
        username = env['pc'][env['default']]['username']
        password = env['pc'][env['default']]['password']
    main_page = browser_init.launch(driver=driver_type, browser=browser_type).go_to_login().login_matplotlibsuccessfully(username, password).countrywide()
    yield main_page
    browser_init.quit()


@pytest.fixture(scope="class")
def get_seller_main_page(browser_init, request, driver_type, browser_type):
    env = load_env()
    try:
        user = request.param
        username = user[env['default']][0]
        password = user[env['default']][1]
    except:
        username = env['seller'][env['default']]['username']
        password = env['seller'][env['default']]['password']
    main_page = browser_init.launch(test_end='seller', driver=driver_type, browser=browser_type).login_successfully(username, password)
    yield main_page

    browser_init.quit()

@pytest.fixture(scope="class")
def get_ipsa_login_page(browser_init, driver_type, browser_type):
    main_page = browser_init.launch(test_end='ipsa', driver=driver_type, browser=browser_type)
    yield main_page
    browser_init.quit()



@pytest.fixture(scope="class")
def get_om_main_page(browser_init, request, driver_type, browser_type):
    env = load_env()
    try:
        user = request.param
        username = user[env['default']][0]
        password = user[env['default']][1]
    except:
        username = env['om'][env['default']]['username']
        password = env['om'][env['default']]['password']
    main_page = browser_init.launch(test_end='om', driver=driver_type, browser=browser_type).login_successfully(username, password)
    yield main_page
    browser_init.quit()


@pytest.fixture(scope="class")
def get_om_main_page_with_no_logged(browser_init, driver_type, browser_type):
    main_page = browser_init.launch(test_end='om', driver=driver_type, browser=browser_type)
    yield main_page
    browser_init.quit()


@pytest.fixture(scope='function', autouse=True)
def capture_screenshot(browser_init):
    yield
    # todo: check sys.exc_info没有获取异常的原因
    # print('sys里有没有错误信息:{0}'.format(sys.exc_info()))
    # if sys.exc_info()[0]:
    screenshot = browser_init.driver.get_screenshot_as_png()
    allure.attach(screenshot, '截图', allure.attachment_type.PNG)

#失败截图
# @pytest.hookimpl(tryfirst=True, hookwrapper=True)
# def pytest_runtest_makereport(item,call):
#     outcome = yield
#     report = outcome.get_result()
#     # 仅仅获取用例call 执行结果是失败的情况，不包含setup和teardown
#     if report.when == 'call' and report.failed:
#         # 添加allure报告截图
#         global browser
#         if browser is None:
#             browser = Browser()
#         screenshot = browser.driver.get_screenshot_as_png()
#         allure.attach(screenshot, '截图', allure.attachment_type.PNG)


def pytest_addoption(parser):
    # driver选项, 用来选择是用本地的driver还是用远程的driver
    parser.addoption(
        "--driver", action="store", default="local", help="local or remote driver"
    )
    # browser选项，用来选择是用chrome还是firefox浏览器来跑测试用例
    parser.addoption(
        "--browser", action="store", default="Chrome", help="chrome or firefox browser"
    )


@pytest.fixture(scope="session")
def driver_type(request):
    return request.config.getoption("--driver")


@pytest.fixture(scope="session")
def browser_type(request):
    return request.config.getoption("--browser")


@pytest.fixture()
def refresh_page(browser_init):
    yield
    return browser_init.driver.refresh()


@pytest.fixture()
def go_back_to_main_page(browser_init, driver_type, browser_type):
    yield
    return browser_init.launch(driver=driver_type, browser=browser_type)


@pytest.fixture(scope="function")
def switch_to_first_window(browser_init):
    handles = browser_init.driver.window_handles
    # try:
    #     WebDriverWait(self.driver, 5).until(EC.new_window_is_opened(handles))
    #     self.driver.switch_to.window(self.driver.window_handles[0])
    # except:
    browser_init.driver.switch_to.window(handles[0])
@pytest.fixture(scope="class")
def get_crm_log_page(browser_init,driver_type,browser_type):
    env = load_env()
    username = env['crm'][env['default']]['username']
    password = env['crm'][env['default']]['password']
    main_page = browser_init.launch(test_end='crm', driver=driver_type, browser=browser_type).login_process(username,password)
    yield main_page
    browser_init.quit()
@pytest.fixture(scope="class")
def get_crm_open(browser_init,driver_type,browser_type):
    main_page = browser_init.launch(test_end='crm', driver=driver_type, browser=browser_type)
    yield main_page
    browser_init.quit()










